﻿using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class ApplicationPagesPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IApplicationPagesView _view;

        public IDiskAccess DiskAccess { get; set; }
        
        public ApplicationPagesPresenter(IApplicationPagesView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }
        
        public ApplicationPagesPresenter(IApplicationPagesView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            Domain.Application app = SiteRepository.GetApplication(_view.ApplicationID);
            _view.SetHeading(app.Name);
            if (!_administrator.HasPermission.ApplicationPages)
                _view.RedirectToPermissionDeniedPage();
        }
        
        public void Initialize()
        {
            SetPages();
            if (_administrator.CurrentSite.MasterTemplates.Count == 0)
                _view.DisplayCreateMasterTemplateAlert();
            else if (_administrator.CurrentSite.DetailTemplates.Count == 0)
                _view.DisplayCreateDetailTemplateAlert();
        }
        
        public void SetPages()
        {
            Domain.Application app = SiteRepository.GetApplication(_view.ApplicationID);
            _view.SetPages(app.Pages);
        }
        
        public void DeletePage(int id)
        {
            var page = (ApplicationPage)PageRepository.GetPage(id);
            var frm = new FileReferencesManager(_administrator, SiteRepository, ContentRepository, DiskAccess);
            frm.RemoveFileReferences(page.GetVirtualPath(), _administrator.CurrentSite.ID);
            page.Site.Pages.Remove(page);
            page.Site = null;
            page.Application.Pages.Remove(page);
            page.Application = null;
            if (page.Folder != null)
            {
                page.Folder.Pages.Remove(page);
                page.Folder = null;
            }
            foreach (PageContentLink pl in page.Content)
            {
                pl.Page = null;
            }
            page.Content.Clear();
            page.DetailTemplate = null;
            page.MasterTemplate = null;
            PageRepository.DeletePage(id);
            SiteRepository.SaveSite(_administrator.CurrentSite);
            DiskAccess.DeleteFile("__System\\ApplicationPages\\" + id + ".aspx");
            SetPages();
        }

        public string GetPagePreviewUrl(int pageID)
        {
            Page page = PageRepository.GetPage(pageID);
            return "http://" + page.Site.Domain + "/" + page.GetVirtualPath();
        }
    }
}